package com.jy.event.service.impl;


import com.jy.event.dao.StoreDao;
import com.jy.event.dao.WebRechargeDao;
import com.jy.event.domain.PaymentStatus;
import com.jy.event.domain.Store;
import com.jy.event.domain.WebRecharge;
import com.jy.event.pay.request.PayRequest;
import com.jy.event.pay.result.PayResult;
import com.jy.event.service.CommissionService;
import com.jy.event.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihu on 2017/10/21.
 */
@Slf4j
@Service
public class CommissionServiceImpl implements CommissionService {


    @Autowired
    SalesUrlUtil salesUrlUtil;

    @Autowired
    WebRechargeDao webRechargeDao;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    StoreDao storeDao;


    @Override
    @Transactional
    public void commission(WebRecharge webRecharge) throws IOException {
        String url = salesUrlUtil.commissionUrl();
        Map map = new HashMap();
        map.put("storeId", webRecharge.getStoreId());
        restTemplate.postForEntity(url, map, String.class);
        webRecharge.setSynchro(true);
        webRechargeDao.save(webRecharge);
    }

    @Override
    @Transactional
    public void checkWebRecharge(WebRecharge webRecharge) throws Exception {
        PayResult queryResult = webQuery(webRecharge.getId());
        if (StringUtils.equals("0", queryResult.getStatus()) &&
                StringUtils.equals("0", queryResult.getResultCode()) &&
                StringUtils.equals("SUCCESS", queryResult.getTradeState())) {
            webRecharge.setStatus(PaymentStatus.SUCCESS);
            webRecharge.setAmount(queryResult.getTotalFee());
            Store store = storeDao.findOne(webRecharge.getStoreId());
            //修改店铺过期时间
            store.setExpireTime(MyDateUtils.getDateDiffYear(store.getExpireTime() != null ?
                    store.getExpireTime() : new Date(), 1));
            storeDao.save(store);
            webRechargeDao.save(webRecharge);
        } else {
            if (!StringUtils.equals("Y", queryResult.getNeed_query())) {
                webRecharge.setStatus(PaymentStatus.FAIL);
                webRechargeDao.save(webRecharge);
            }
        }
    }


    public PayResult webQuery(String id) throws Exception {
        //初始化请求实体
        PayRequest payRequest = new PayRequest("unified.trade.query",
                "399520227696", id);
        payRequest.setUpSign(ObjectToMap.objectToMap(payRequest), "8de420f99019656a1f6da0412b49f01b");
        log.info("webQuery PayRequest:{}", payRequest);
        String xml = restTemplate.postForEntity(Contens.ALI_QR_CODE, payRequest.toXML(), String.class).getBody();
        PayResult queryResult = XmlUtils.fromXML(xml, PayResult.class);
        return queryResult;
    }
}
